package cn.felord.security.autoconfigure.authentication;


import cn.felord.security.autoconfigure.authentication.captcha.CaptchaLoginFilterConfigurer;
import cn.felord.security.autoconfigure.authentication.miniapp.douyin.DouyinMiniAppLoginFilterConfigurer;
import cn.felord.security.autoconfigure.authentication.miniapp.wechat.WechatMiniAppLoginFilterConfigurer;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.web.DefaultSecurityFilterChain;


/**
 * The type Login filter security configurer.
 *
 * @param <H> the type parameter
 */
public class LoginFilterSecurityConfigurer<H extends HttpSecurityBuilder<H>> extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, H> {
    private CaptchaLoginFilterConfigurer<H> captchaLoginFilterConfigurer;
    private WechatMiniAppLoginFilterConfigurer<H> wechatMiniAppLoginFilterConfigurer;
    private DouyinMiniAppLoginFilterConfigurer<H> douyinMiniAppLoginFilterConfigurer;

    /**
     * Captcha login captcha login filter configurer.
     *
     * @return the captcha login filter configurer
     */
    public CaptchaLoginFilterConfigurer<H> captchaLogin() {
        return lazyInitCaptchaLoginFilterConfigurer();
    }

    /**
     * Captcha login login filter security configurer.
     *
     * @param captchaLoginFilterConfigurerCustomizer the captcha login filter configurer customizer
     * @return the login filter security configurer
     */
    public LoginFilterSecurityConfigurer<H> captchaLogin(Customizer<CaptchaLoginFilterConfigurer<H>> captchaLoginFilterConfigurerCustomizer) {
        captchaLoginFilterConfigurerCustomizer.customize(lazyInitCaptchaLoginFilterConfigurer());
        return this;
    }

    /**
     * Mini app login mini app login filter configurer.
     *
     * @return the mini app login filter configurer
     */
    public WechatMiniAppLoginFilterConfigurer<H> wechatMiniAppLogin() {
        return lazyInitWechatMiniAppLoginFilterConfigurer();
    }

    /**
     * Mini app login login filter security configurer.
     *
     * @param wechatMiniAppLoginFilterConfigurerCustomizer  wechatMiniAppLoginFilterConfigurerCustomizer
     * @return the login filter security configurer
     */
    public LoginFilterSecurityConfigurer<H> wechatMiniAppLogin(Customizer<WechatMiniAppLoginFilterConfigurer<H>> wechatMiniAppLoginFilterConfigurerCustomizer) {
        wechatMiniAppLoginFilterConfigurerCustomizer.customize(lazyInitWechatMiniAppLoginFilterConfigurer());
        return this;
    }

    /**
     * Douyin mini app login douyin mini app login filter configurer.
     *
     * @return the douyin mini app login filter configurer
     */
    public DouyinMiniAppLoginFilterConfigurer<H> douyinMiniAppLogin() {
        return lazyInitDouyinMiniAppLoginFilterConfigurer();
    }

    /**
     * Douyin mini app login login filter security configurer.
     *
     * @param douyinMiniAppLoginFilterConfigurerCustomizer the douyin mini app login filter configurer customizer
     * @return the login filter security configurer
     */
    public LoginFilterSecurityConfigurer<H> douyinMiniAppLogin(Customizer<DouyinMiniAppLoginFilterConfigurer<H>> douyinMiniAppLoginFilterConfigurerCustomizer) {
        douyinMiniAppLoginFilterConfigurerCustomizer.customize(lazyInitDouyinMiniAppLoginFilterConfigurer());
        return this;
    }
    @Override
    public void init(H builder) {
        if (captchaLoginFilterConfigurer != null) {
            captchaLoginFilterConfigurer.init(builder);
        }
        if (wechatMiniAppLoginFilterConfigurer != null) {
            wechatMiniAppLoginFilterConfigurer.init(builder);
        }
        if (douyinMiniAppLoginFilterConfigurer != null) {
            douyinMiniAppLoginFilterConfigurer.init(builder);
        }
    }

    @Override
    public void configure(H builder) throws Exception {
        if (captchaLoginFilterConfigurer != null) {
            captchaLoginFilterConfigurer.configure(builder);
        }
        if (wechatMiniAppLoginFilterConfigurer != null) {
            wechatMiniAppLoginFilterConfigurer.configure(builder);
        }
        if (douyinMiniAppLoginFilterConfigurer != null) {
            douyinMiniAppLoginFilterConfigurer.configure(builder);
        }
    }

    private CaptchaLoginFilterConfigurer<H> lazyInitCaptchaLoginFilterConfigurer() {
        if (captchaLoginFilterConfigurer == null) {
            this.captchaLoginFilterConfigurer = new CaptchaLoginFilterConfigurer<>(this);
        }
        return captchaLoginFilterConfigurer;
    }

    private WechatMiniAppLoginFilterConfigurer<H> lazyInitWechatMiniAppLoginFilterConfigurer() {
        if (wechatMiniAppLoginFilterConfigurer == null) {
            this.wechatMiniAppLoginFilterConfigurer = new WechatMiniAppLoginFilterConfigurer<>(this);
        }
        return wechatMiniAppLoginFilterConfigurer;
    }

    private DouyinMiniAppLoginFilterConfigurer<H> lazyInitDouyinMiniAppLoginFilterConfigurer() {
        if (douyinMiniAppLoginFilterConfigurer == null) {
            this.douyinMiniAppLoginFilterConfigurer = new DouyinMiniAppLoginFilterConfigurer<>(this);
        }
        return douyinMiniAppLoginFilterConfigurer;
    }
}
